﻿#pragma once

#include "Misc/StdHead.h"
#include "Object/Object.h"
#include "Render/Texture/TextureFormat.h"
#include "TextureCube.m.h"


class CRenderTextureCube;

//立方体贴图
//由六个大小格式一样的贴图组成的立方体
//目前这六张图的方向存在问题，作为天空盒从内部看，存在左右反转的问题
RCLASS()
class WHENGINE_API RTextureCube : public RObject
{
    ROBJECT
public:

    struct STextureCubeCreateInfo
    {
        TVector<uint8_t> Front;
        TVector<uint8_t> Back;
        TVector<uint8_t> Left;
        TVector<uint8_t> Right;
        TVector<uint8_t> Top;
        TVector<uint8_t> Bottom;
        uint64_t Width;
        uint64_t Height;
        uint64_t Pitch;
        ETextureFormat Format;
    };

    static TSharedObjectPtr<RTextureCube> CreateFromCreateInfo(STextureCubeCreateInfo&& InCreateInfo, RObject* InOwner = nullptr);

    RTextureCube();

    int GetWidth()
    {
        return Width;
    }

    int GetHeight()
    {
        return Height;
    }

    ETextureFormat GetFormat()
    {
        return Format;
    }
protected:

    RFIELD()
    int64_t Width;
    RFIELD()
    int64_t Height;

    //Bytes num per image row
    RFIELD()
    int64_t Pitch;

    RFIELD(SerializeAsBinary)
    TVector<uint8_t> Front;

    RFIELD(SerializeAsBinary)
    TVector<uint8_t> Back;

    RFIELD(SerializeAsBinary)
    TVector<uint8_t> Left;

    RFIELD(SerializeAsBinary)
    TVector<uint8_t> Right;

    RFIELD(SerializeAsBinary)
    TVector<uint8_t> Top;

    RFIELD(SerializeAsBinary)
    TVector<uint8_t> Bottom;

    RFIELD()
    ETextureFormat Format=ETextureFormat::Undefined;

public:
    //同步到渲染线程，仅当数据变脏时有效
    void SyncToRenderThread();

    //渲染线程对应的对象
    std::shared_ptr<CRenderTextureCube> GetRenderTexture();
protected:
    std::shared_ptr<CRenderTextureCube> RenderTexture;

    //数据是否变脏
    bool bDirty=false;
    //强制同步到渲染线程，不管数据是否变脏
    void ForceSyncToRenderThread();
};